﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.IO;
using Bitis_KeToanThue_Pro_DungChung.Common;
using Bitis_KeToanThue_Pro_QuanTri.AccessDatabase;
using Telerik.WinControls.Data;
using System.Reflection;

namespace Bitis_KeToanThue_Pro_QuanTri.Form
{
    public partial class frmEditTree : Telerik.WinControls.UI.RadForm
    {
        PhanNhomMenu pn = new PhanNhomMenu();
        int manhomload = 0;
        public frmEditTree(int manhom)
        {
            InitializeComponent();
            manhomload = manhom;
        }

        private void loaddulieu(int manhom)
        {
             DataTable table = new DataTable();
             table = pn.loadtreeshowtest(manhom);
            this.radTreeView1.DataSource = table;
            this.radTreeView1.DisplayMember = "TenMenu";
            this.radTreeView1.ChildMember = "MenuID";
            this.radTreeView1.ParentMember = "MenuCha";
            this.radTreeView1.ValueMember = "MenuID";
            this.radTreeView1.ExpandAll();
           
         
      
        }
        private void removetree()
        {
            this.radTreeView1.Nodes.Clear();
        }
        private void frmEditTree_Load(object sender, EventArgs e)
        {
            loaddulieu(manhomload);
        }
        private void soanlai()
        {
            txtDienGiai.Text = string.Empty;
            txtThuTu.Value = 0;
            txtMenuID.Text = string.Empty;
        }
        private void radTreeView1_NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
        {
            DataRowView rowView = (DataRowView)e.Node.DataBoundItem;
            string photo = ClassXuLyChung.GetSafeString(rowView["Icon"]);
            string tenmnu = (string)rowView["TenMenu"];
            int thutuxuathien = (int)rowView["ThuTuXuatHien"];
            if (!string.IsNullOrEmpty(photo))
            {
                e.NodeElement.ImageElement.Image = ClassXuLyChung.ConvertByeToImage((byte[])rowView["Icon"]);
            }
            else
            {
                e.NodeElement.ImageElement.Image = null;
            }
         
        
          //  e.NodeElement.ContentElement.ForeColor = Color.Blue;
            e.NodeElement.ContentElement.Text = String.Format("{0} : Thứ Tự Xuất Hiện ( {1} )", tenmnu, thutuxuathien);
           
         
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtMenuID.Text != "" && txtDienGiai.Text != "")
            {
                if (pn.updatethutu(manhomload, ClassXuLyChung.GetSafeInt(txtMenuID.Text), ClassXuLyChung.GetSafeInt(txtThuTu.Value)) == 1)
                {
                    removetree();
                    RadMessageBox.Show("Cập Nhật Thành Công ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                    soanlai();
                    loaddulieu(manhomload);
                }
            }
            else
            {
                RadMessageBox.Show("Không tồn tại dữ liệu ", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
            }
        }

        private void radTreeView1_NodeMouseClick(object sender, RadTreeViewEventArgs e)
        {
            DataRowView rowView = (DataRowView)e.Node.DataBoundItem;
            txtDienGiai.Text = (string)rowView["TenMenu"];
            txtThuTu.Value = (int)rowView["ThuTuXuatHien"];
            txtMenuID.Text = ClassXuLyChung.GetSafeInt(rowView["MenuID"]).ToString();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            frmViewMenu frm = new frmViewMenu(manhomload);
            frm.ShowDialog();
        }

    }
}
